package win.timloong.android.carwiki.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

import me.nereo.multi_image_selector.MultiImageSelectorActivity;
import win.timloong.android.carwiki.R;
import win.timloong.android.carwiki.adapter.QuanziAdapter;
import win.timloong.android.carwiki.bean.Quanzi;

/**
 * 推荐
 */
public class QuanziFragment extends Fragment implements View.OnClickListener {

    private ArrayList<String> defaultDataArray = new ArrayList<String>();
    private int REQUEST_CODE = 0;
    private int RESULT_OK = -1;

    private RecyclerView recyclerView;

    QuanziAdapter quanziAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_quanzi, container, false);
        inflate.findViewById(R.id.quanzi_top_fabiao).setOnClickListener(this);
        recyclerView = (RecyclerView) inflate.findViewById(R.id.quanzi_recyclerview);


        ArrayList<Quanzi> arrayList = new ArrayList<>();
        String[] urls = new String[]{
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/910d6671c036418bc21de865a6fa7807_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/b1a055e5fbfdc2956dabb444d4a12960_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/3f873023db3c349f892c2e9ca66f3225_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/09fae8dc4c14b6bb7e02dbb5643e24d7_1000x750.jpg"
        };

        String[] urls2 = new String[]{
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/910d6671c036418bc21de865a6fa7807_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/b1a055e5fbfdc2956dabb444d4a12960_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/3f873023db3c349f892c2e9ca66f3225_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/09fae8dc4c14b6bb7e02dbb5643e24d7_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/d795541d067321b5340a9e812f730d2f_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/f3c3f8ce691332f6aad34523568c56a1_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/d492ec81d6e1921ab736a784095b83f7_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/817bbe9f1e17af46fea0f6b8a9873ada_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/a5d28ebccbe44c16bd4e4dbe38ca416b_1000x750.jpg",
                "http://img.club.pchome.net/kdsarticle/2013/11small/21/77e088c4712f29c41a9c1a143c88378f_1000x750.jpg"

        };
        arrayList.add(new Quanzi("111", "我的昵称", "1小时前", "还不错吧", urls));
        arrayList.add(new Quanzi("222", "我的昵称2", "2小时前", "还不错吧", urls2));
        arrayList.add(new Quanzi("2222", "我的昵称3", "3小时前", "还不错吧", urls));
        quanziAdapter = new QuanziAdapter(arrayList);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        recyclerView.setAdapter(quanziAdapter);



        return inflate;
    }


    @Override
    public void onClick(View view) {

        //发动态选图片:
        //调用方式1
//        MultiImageSelector.create(view.getContext())
//                .showCamera(true) // show camera or not. true by default
//                .count(9) // max select image size, 9 by default. used width #.multi()
//                .single() // single mode
//                .multi() // multi mode, default mode;
//                .origin(defaultDataArray) // original select data set, used width #.multi()
//                .start(this, REQUEST_CODE);

        //调用方式2
        Intent intent = new Intent(view.getContext(), MultiImageSelectorActivity.class);
        // whether show camera
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
        // max select image amount
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9);
        // select mode (MultiImageSelectorActivity.MODE_SINGLE OR MultiImageSelectorActivity.MODE_MULTI)
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
        // default select images (support array list)
//        intent.putStringArrayListExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, defaultDataArray);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            defaultDataArray = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
           Toast.makeText(getContext(), "已选图片路径" + Arrays.toString(defaultDataArray.toArray()), Toast.LENGTH_LONG).show();


//
//            if (defaultDataArray.size()>0){
//                String[] urls = new String[defaultDataArray.size()];
//                for (int i=0;i<defaultDataArray.size();i++){
//                  urls[i]=(String) defaultDataArray.get(i);
//                }
//
//                quanziAdapter.addData(new Quanzi("2222", "我的昵称3", "3小时前", "还不错吧", urls));
//            }
        }
    }
}
